package com.lm.liaoming.tool.choosebirthday;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;


import com.lm.liaoming.R;
import com.lm.liaoming.tool.DateUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by Administrator on 2016/7/11 0011.
 */
public class MyPopUpWindowChooseDate extends PopupWindow {
    public static int ROUTINE = 1;//常规模式
    public static int ONLY_YEARS = 3;//常规模式


    private View mView;
    private TextView finish;
    public static String sex;
    private List<String> sexData;
    private LoopView chooseSex;
    private Context context;


    public static String year;
    public static String month;
    public static String day;
    private LoopView lpDays;
    ArrayList<String> days = new ArrayList<String>();
    private int currentYear;
    private List<Integer> yearNumber;
    private List<Integer> monthNumber;
    private List<Integer> daysNumber;
    private int positionYear;
    private int positionMonth;
    private int positionDay;
    private LoopView lpMonth;
    private LoopView lpYear;

    private long birthLong;
    private int type;
    private int currentMouth;
    private int currentDate;

    public MyPopUpWindowChooseDate(final Activity context, View.OnClickListener clickListener, long birth) {
        super(context);
        this.context = context;
        this.birthLong = birth;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = inflater.inflate(R.layout.choose_birth_diaolog, null);

        finish = mView.findViewById(R.id.user_info_select_finish);
        chooseSex = mView.findViewById(R.id.user_info_choose_sex);
        TextView tvcancle = mView.findViewById(R.id.btn_cancle);
        tvcancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        lpYear = mView.findViewById(R.id.year);
        lpDays = mView.findViewById(R.id.days);
        lpMonth = mView.findViewById(R.id.month);
        lpYear.setNotLoop();
        lpDays.setNotLoop();
        lpMonth.setNotLoop();
//        age.setEditUserInfoStyle();   //按照设计的颜色
//        constellation.setEditUserInfoStyle();

        yearNumber = new ArrayList<>();
        monthNumber = new ArrayList<>();
        daysNumber = new ArrayList<>();


        initLoopView();
        // 设置按钮监听
        finish.setOnClickListener(clickListener);

        //设置PopupWindow的View
        this.setContentView(mView);
        //设置PopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置PopupWindow弹出窗体的高
        this.setHeight(context.getResources().getDimensionPixelOffset(R.dimen.user_info_dialog_height));
        //设置PopupWindow弹出窗体可点击
        this.setFocusable(true);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xeef6f6f6);
//        设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        WindowManager.LayoutParams params = context.getWindow().getAttributes();
        params.alpha = 0.3f;

        context.getWindow().setAttributes(params);
        this.setAnimationStyle(R.style.livePopStyle);
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams params = context.getWindow().getAttributes();
                params.alpha = 1f;
                context.getWindow().setAttributes(params);
            }
        });

        Calendar c = Calendar.getInstance();
        currentYear = c.get(Calendar.YEAR);
        currentMouth = c.get(Calendar.MONTH);
        Log.e("--------------", currentMouth + "");
        setData();
        showYears();
        showDays(1);
        show(1);
    }

    public MyPopUpWindowChooseDate(final Activity context, int type, View.OnClickListener clickListener, long birth) {
        super(context);
        if (context == null) {
            return;
        }
        if (context.isFinishing()) {
            return;
        }
        this.context = context;
        this.birthLong = birth;
        this.type = type;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = inflater.inflate(R.layout.choose_birth_diaolog, null);

        finish = mView.findViewById(R.id.user_info_select_finish);
        chooseSex = mView.findViewById(R.id.user_info_choose_sex);
        TextView tvcancle = mView.findViewById(R.id.btn_cancle);
        tvcancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        lpYear = mView.findViewById(R.id.year);
        lpDays = mView.findViewById(R.id.days);
        lpMonth = mView.findViewById(R.id.month);
        lpYear.setNotLoop();
        lpDays.setNotLoop();
        lpMonth.setNotLoop();
//        age.setEditUserInfoStyle();   //按照设计的颜色
//        constellation.setEditUserInfoStyle();

        yearNumber = new ArrayList<>();
        monthNumber = new ArrayList<>();
        daysNumber = new ArrayList<>();


        initLoopView();
        // 设置按钮监听
        finish.setOnClickListener(clickListener);

        //设置PopupWindow的View
        this.setContentView(mView);
        //设置PopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置PopupWindow弹出窗体的高
        this.setHeight(context.getResources().getDimensionPixelOffset(R.dimen.user_info_dialog_height));
        //设置PopupWindow弹出窗体可点击
        this.setFocusable(true);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xeef6f6f6);
//        设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        WindowManager.LayoutParams params = context.getWindow().getAttributes();
        params.alpha = 0.3f;

        context.getWindow().setAttributes(params);
        this.setAnimationStyle(R.style.livePopStyle);
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams params = context.getWindow().getAttributes();
                params.alpha = 1f;
                context.getWindow().setAttributes(params);
            }
        });

        Calendar c = Calendar.getInstance();
        currentYear = c.get(Calendar.YEAR);
        currentMouth = c.get(Calendar.MONTH);
        currentDate = c.get(Calendar.DATE);
        setData();
        showYears();
        if (type == 1) {
//         showMonths();
            showCuYUe();
            showDays(1);
            show(1);
        } else {
            showCuYUe();
            showDays(1);
            show(1);
            lpMonth.setVisibility(View.GONE);
            lpDays.setVisibility(View.GONE);
        }

    }

    private void initLoopView() {
        lpYear.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                year = yearNumber.get(index) + "";
            }
        });
        lpMonth.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                month = monthNumber.get(index) + "";
            }
        });
        lpDays.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                day = daysNumber.get(index) + "";
            }
        });
    }


    /*显示年的最大月份*/
    private void showCuYUe() {
        final ArrayList<String> months = new ArrayList<>();
        monthNumber.clear();
        for (int i = 1; i < currentMouth + 2; i++) {
            monthNumber.add(i);
            if (i < 10) {
                months.add("0" + i + "月");
            } else {
                months.add(i + "月");
            }
        }
        lpMonth.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                positionMonth = index;
                show(1);
            }
        });
        lpMonth.setItems(months);
        //设置初始位置
        lpMonth.setInitPosition(positionMonth);
        //设置字体大小
        lpMonth.setTextSize(25);
    }

    ArrayList<String> years;

    private void showYears() {

        years = new ArrayList<>();
        for (int i = 1970; i <= currentYear; i++) {
            years.add(i + "年");
            yearNumber.add(i);
        }

        lpYear.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                if (type == 1) {
                    positionYear = index;
                    lpYear.invalidate();
                    String s = years.get(index);
                    if (s.equals(currentYear + "年")) {
                        showCuYUe();
                        show(1);
                    } else {
                        showMonths();
                        show(0);

                    }
                } else {
                    if (index == yearNumber.size() - 1) {
                        positionYear = index;
                        lpYear.invalidate();
                    } else {
                        positionYear = index;
                        lpYear.invalidate();
                        String s = years.get(index);
                        if (s.equals(currentYear + "年")) {
                            showCuYUe();
                            show(1);
                        } else {
                            showMonths();
                            show(0);
                        }

                    }
                }

            }
        });
//        设置原始数据
        lpYear.setItems(years);
        //设置初始位置
        lpYear.setInitPosition(positionYear);
        //设置字体大小
        lpYear.setTextSize(25);
    }

    private void showMonths() {
        monthNumber.clear();
        final ArrayList<String> months = new ArrayList<>();
        for (int i = 1; i < 13; i++) {
            monthNumber.add(i);
            if (i < 10) {
                months.add("0" + i + "月");
            } else {
                months.add(i + "月");
            }
        }

        lpMonth.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                positionMonth = index;
                show(0);
            }
        });
//        设置原始数据
        lpMonth.setItems(months);
        //设置初始位置
        lpMonth.setInitPosition(positionMonth);
        //设置字体大小
        lpMonth.setTextSize(25);
    }

    private void showDays(final int type) {
        lpDays.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                positionDay = index;
                show(type);
            }
        });
        show(type);
        //设置原始数据
        lpDays.setItems(days);
        //设置初始位置
        lpDays.setInitPosition(positionDay);
        //设置字体大小
        lpDays.setTextSize(25);
    }

    private void show(int type) {
        lpMonth.invalidate();
        lpYear.invalidate();
        lpDays.invalidate();
        try {
            Integer integer = yearNumber.get(positionYear);
            Integer integer1 = monthNumber.get(positionMonth);

            if (integer == currentYear && integer1 == (currentMouth + 1)) {
                cuyuedate();
            } else {
                daysUpdate();
            }
        } catch (Exception e) {
            daysUpdate();
        }

    }

    private void daysUpdate() {
        days.clear();
        daysNumber.clear();
        if (1 == positionMonth + 1 || 3 == positionMonth + 1 || 5 == positionMonth + 1 || 7 == positionMonth + 1 || 8 == positionMonth + 1 || 10 == positionMonth + 1 || 12 == positionMonth + 1) {
            for (int i = 1; i < 32; i++) {
                daysNumber.add(i);
                if (i < 10) {
                    days.add("0" + i + "日");
                } else {
                    days.add(i + "日");
                }
            }
        } else if (4 == positionMonth + 1 || 6 == positionMonth + 1 || 9 == positionMonth + 1 || 11 == positionMonth + 1) {
            for (int i = 1; i < 31; i++) {
                daysNumber.add(i);
                if (i < 10) {
                    days.add("0" + i + "日");
                } else {
                    days.add(i + "日");
                }
            }
        } else {
            if (((positionYear % 4 == 0) && (positionYear % 100 != 0)) || (positionYear % 400 == 0)) {
                for (int i = 1; i < 30; i++) {
                    daysNumber.add(i - 1);
                    if (i < 10) {
                        days.add("0" + i + "日");
                    } else {
                        days.add(i + "日");
                    }
                }
            } else {
                for (int i = 1; i < 29; i++) {
                    daysNumber.add(i - 1);
                    if (i < 10) {
                        days.add("0" + i + "日");
                    } else {
                        days.add(i + "日");
                    }
                }
            }
        }
    }

    private void cuyuedate() {
        days.clear();
        daysNumber.clear();
        if (1 == positionMonth + 1 || 3 == positionMonth + 1 || 5 == positionMonth + 1 || 7 == positionMonth + 1 || 8 == positionMonth + 1 || 10 == positionMonth + 1 || 12 == positionMonth + 1) {
            for (int i = 1; i < 32; i++) {
                if (i <= currentDate) {
                    daysNumber.add(i);
                    if (i < 10) {
                        days.add("0" + i + "日");
                    } else {
                        days.add(i + "日");
                    }
                }
            }
        } else if (4 == positionMonth + 1 || 6 == positionMonth + 1 || 9 == positionMonth + 1 || 11 == positionMonth + 1) {
            for (int i = 1; i < 31; i++) {
                if (i <= currentDate) {
                    daysNumber.add(i);
                    if (i < 10) {
                        days.add("0" + i + "日");
                    } else {
                        days.add(i + "日");
                    }
                }
            }
        } else {
            if (((positionYear % 4 == 0) && (positionYear % 100 != 0)) || (positionYear % 400 == 0)) {
                for (int i = 1; i < 30; i++) {
                    if (i <= currentDate) {
                        daysNumber.add(i - 1);
                        if (i < 10) {
                            days.add("0" + i + "日");
                        } else {
                            days.add(i + "日");
                        }
                    }
                }
            } else {
                for (int i = 1; i < 29; i++) {
                    if (i <= currentDate) {
                        daysNumber.add(i - 1);
                        if (i < 10) {
                            days.add("0" + i + "日");
                        } else {
                            days.add(i + "日");
                        }
                    }
                }
            }
        }
    }

    private void setData() {
        try {
//            Intent intent = getIntent();
//            birthLong = intent.getLongExtra("birthday", 1l);

            positionDay = Integer.parseInt(DateUtil.getDays(birthLong)) - 1;
            positionMonth = Integer.parseInt(DateUtil.getMonth(birthLong)) - 1;
            positionYear = Integer.parseInt(DateUtil.getYear(birthLong)) - 1970;

        } catch (Exception e) {
        }


    }

    public static String chooseTime;

    public String getYear() {
        String s = years.get(lpYear.getSelectedItem());
        return s;
    }

    public String getchooseTime() {
        chooseTime = yearNumber.get(lpYear.getSelectedItem()) + "";
        if (type == ONLY_YEARS) {
            return chooseTime;
        }
        if (monthNumber.get(lpMonth.getSelectedItem()) < 10) {
            chooseTime = chooseTime + "/0" + monthNumber.get(lpMonth.getSelectedItem());
        } else {
            chooseTime = chooseTime + "/" + monthNumber.get(lpMonth.getSelectedItem());
        }
        /**
         * 由于2月份儿返回值总是少一天  所以在这里手动加 1
         */
        if (2 == monthNumber.get(lpMonth.getSelectedItem())) {
            if (daysNumber.get(lpDays.getSelectedItem()) < 9) {
                chooseTime = chooseTime + "/0" + ((daysNumber.get(lpDays.getSelectedItem())) + 1);
//            }else if (daysNumber.get(lpDays.getSelectedItem()) == 10){
//                chooseTime = chooseTime + "-" + 10;
            } else {
                chooseTime = chooseTime + "/" + ((daysNumber.get(lpDays.getSelectedItem())) + 1);
            }
        } else {
            if (daysNumber.get(lpDays.getSelectedItem()) < 10) {
                chooseTime = chooseTime + "/0" + ((daysNumber.get(lpDays.getSelectedItem())));
            } else {
                chooseTime = chooseTime + "/" + ((daysNumber.get(lpDays.getSelectedItem())));
            }
        }

        return chooseTime;
    }

    public String getchooseTimes() {
        chooseTime = yearNumber.get(lpYear.getSelectedItem()) + "";
        if (type == ONLY_YEARS) {
            return chooseTime;
        }
        if (monthNumber.get(lpMonth.getSelectedItem()) < 10) {
            chooseTime = chooseTime + "-0" + monthNumber.get(lpMonth.getSelectedItem());
        } else {
            chooseTime = chooseTime + "-" + monthNumber.get(lpMonth.getSelectedItem());
        }
        /**
         * 由于2月份儿返回值总是少一天  所以在这里手动加 1
         */
        if (2 == monthNumber.get(lpMonth.getSelectedItem())) {
            if (daysNumber.get(lpDays.getSelectedItem()) < 9) {
                chooseTime = chooseTime + "-0" + ((daysNumber.get(lpDays.getSelectedItem())) + 1);
//            }else if (daysNumber.get(lpDays.getSelectedItem()) == 10){
//                chooseTime = chooseTime + "-" + 10;
            } else {
                chooseTime = chooseTime + "-" + ((daysNumber.get(lpDays.getSelectedItem())) + 1);
            }
        } else {
            if (daysNumber.get(lpDays.getSelectedItem()) < 10) {
                chooseTime = chooseTime + "-0" + ((daysNumber.get(lpDays.getSelectedItem())));
            } else {
                chooseTime = chooseTime + "-" + ((daysNumber.get(lpDays.getSelectedItem())));
            }
        }

        return chooseTime;
    }
}